home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 November / Chip Kasım 2001.iso / prog / cdcode / smartsdk / setup.exe / stsdk.msi / Conversion.cls.C6DB95E3_9157_4174_9574_EF0371EC54EB < prev    next >
Encoding:
Visual Basic class definition  |  2001-04-13  |  10.2 KB  |  221 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "ConversionRecognizer"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. Option Explicit
  15. Implements ISmartTagRecognizer
  16.  
  17. Const METRICURI = "metricSystem#unit"
  18. Private Property Get ISmartTagRecognizer_ProgId() As String
  19.     ' Create a unique identifier for this recognizer
  20.     ' that corresponds to the ProgID of this dll.
  21.  
  22.     ISmartTagRecognizer_ProgId = "Conversion.ConversionRecognizer"
  23. End Property
  24. Private Property Get ISmartTagRecognizer_Name(ByVal LocaleID As Long) As String
  25.     ' Add a short phrase that describes this recognizer that will be
  26.     ' shown in the Tools>Autocorrect Option>Smart Tags
  27.     ' dialog box in both Word and Excel.
  28.     ISmartTagRecognizer_Name = "Metric value recognizer"
  29. End Property
  30. Private Property Get ISmartTagRecognizer_Desc(ByVal LocaleID As Long) As String
  31.     ' Create a longer description of this recognizer.
  32.     ISmartTagRecognizer_Desc = "Recognizes metric units"
  33. End Property
  34. Private Property Get ISmartTagRecognizer_SmartTagCount() As Long
  35.     ' Specify the number of smart tag types this recognizer supports.
  36.     ' 1 in this case.
  37.     ISmartTagRecognizer_SmartTagCount = 1
  38. End Property
  39. Private Property Get ISmartTagRecognizer_SmartTagName(ByVal SmartTagID As Long) As String
  40.     ' Provide the name of the smart tag type supported.
  41.     ' SmartTag names are always in the format of namespaceURI#tagname.
  42.     If (SmartTagID = 1) Then
  43.         ISmartTagRecognizer_SmartTagName = METRICURI
  44.     End If
  45. End Property
  46. Private Property Get ISmartTagRecognizer_SmartTagDownloadURL(ByVal SmartTagID As Long) As String
  47.     ' For this particular smart tag type, there is a Web site
  48.     ' to support a smart tag download URL. Note that "http://metricConversion"
  49.     ' supplied below is a hypothetical Web site used to
  50.     ' demonstrate the Check for New Actions... menu item.
  51.  
  52.     ISmartTagRecognizer_SmartTagDownloadURL = "http://metricConversion"
  53. End Property
  54. Public Sub ISmartTagRecognizer_Recognize(ByVal Text As String, ByVal DataType As SmartTagLib.IF_TYPE, ByVal LocaleID As Long, ByVal RecognizerSite As SmartTagLib.ISmartTagRecognizerSite)
  55. ' Recognize numbers followed by the symbol l or m for liters or meters.
  56.     Dim i As Integer
  57.     Dim j As Integer
  58.     Dim match As Integer
  59.     Dim index As Integer
  60.     Dim quantity As Integer
  61.     Dim termlen As Integer
  62.     Dim propbag As SmartTagLib.ISmartTagProperties
  63.     Dim words As Variant
  64.     Dim length As Integer
  65.     Dim position As Integer
  66.     
  67.     ' Catch any errors and display the VB error for debugging purposes.
  68.     On Error GoTo bail
  69.     
  70.     ' Check for characters used in word to mark empty space in the buffer.
  71.     ' If any of those characters are present at the end of Text, then
  72.     ' eliminate them.
  73.     length = Len(Text)
  74.     If (length > 0) Then
  75.         position = InStr(1, Text, ChrW(-4))
  76.     Else
  77.         position = 0
  78.     End If
  79.     
  80.     While (length > 0 And position > 0)
  81.         Mid$(Text, position, 1) = " "
  82.         length = length - 1
  83.         If (length > 0) Then
  84.             position = InStr(1, Text, ChrW(-4))
  85.         Else
  86.             position = 0
  87.         End If
  88.         position = InStr(1, Text, ChrW(-4))
  89.     Wend
  90.     Text = RTrim(Text)
  91.     
  92.     ' now trim off the carriage return at the end of the string if there is one
  93.     If (Right(Text, 1) = ChrW(13)) Then
  94.         Text = Left(Text, length - 1)
  95.     End If
  96.         
  97.     ' Convert to lowercase to handle M the same as m
  98.     Text = LCase(Text)
  99.     
  100.     ' When getting data from Excel, there is no need to check anything but the
  101.     ' entire string, since Excel supports only smart tags that comprise the entire
  102.     ' contents of the cell.
  103.     If (DataType = IF_TYPE_CELL) Then
  104.         ' Check if the whole string starts with a digit.
  105.         If (Mid(Text, 1, 1) >= "0" And Mid(Text, 1, 1) <= "9") Then
  106.             match = 0
  107.             termlen = Len(Text)
  108.             ' If it starts with a digit, check if it ends with "l" or "m"
  109.             If (Mid(Text, termlen, 1) = "m") Then
  110.                 match = 1
  111.                 ' Ask the recognizer site for a property bag.
  112.                 Set propbag = RecognizerSite.GetNewPropertyBag
  113.                 ' Write the name of the unit
  114.                 propbag.Write "unit", "meters"
  115.                 ' Setting the style property demonstrates how properties
  116.                 ' of a document viewed in a web browser can be controlled.
  117.                 ' When this smart tag is viewed in IE the text color will be
  118.                 ' navy. Note that you could also put an onclick event here or
  119.                 ' set other properties that will change the behavior within IE.
  120.                 propbag.Write "style", "color:navy"
  121.             ElseIf (Mid(Text, termlen, 1) = "l") Then
  122.                 match = 1
  123.                 Set propbag = RecognizerSite.GetNewPropertyBag
  124.                 propbag.Write "unit", "liters"
  125.                 propbag.Write "style", "color:red"
  126.             End If
  127.             ' If there is a match, commit the smart tag.
  128.             If (match = 1) Then
  129.                 ' If the string starts with a digit and ends with a metric unit,
  130.                 ' then check if all the intervening characters are also digits.
  131.                 If (termlen > 1) Then
  132.                     For j = 2 To (termlen - 1)
  133.                         If (Mid(Text, j, 1) < "0" Or Mid(Text, j, 1) > "9") Then
  134.                             match = 0
  135.                         End If
  136.                     Next
  137.                 End If
  138.                 If (match = 1) Then
  139.                     quantity = Mid(Text, 1, termlen - 1)
  140.                     propbag.Write "quantity", quantity
  141.                     ' Commit the smart tag to the application.
  142.                     RecognizerSite.CommitSmartTag METRICURI, 1, termlen, propbag
  143.                 Else
  144.                     Set propbag = Nothing
  145.                 End If
  146.             End If
  147.         End If
  148.     ' Check to see whether text is coming in a paragraph at a time, as happens with Word.
  149.     ElseIf (DataType = IF_TYPE_PARA) Then
  150.         ' Split the string into words.
  151.         words = Split(Text, " ", -1, vbTextCompare)
  152.         ' Set index to refer to the beginning of the string.
  153.         index = 1
  154.         For i = LBound(words) To UBound(words)
  155.             ' Get the position of the current word in the original Text.
  156.             index = InStr(index, Text, words(i), vbTextCompare)
  157.             ' Metric units must start with a number and have the unit (e.g. m for meters attached) for simplicity.
  158.             ' Note that this recognizer is fairly aggressive: it labels any token that starts with
  159.             ' a number and ends with either "l" or "m" as a metric unit. This is fine for tokens like
  160.             ' 125m and 72l but it misrecognizes 72km as being in meters and 58gal as being in liters. Later on
  161.             ' there is a check to ensure that all of the characters between the first and last are digits.
  162.             ' This makes the recognizer less aggressive, but eliminates valid matches, like 1.9m.
  163.             If (Mid(words(i), 1, 1) >= "0" And Mid(words(i), 1, 1) <= "9") Then
  164.                 termlen = Len(words(i))
  165.                 match = 0
  166.                 ' Now just check that the string ends in a metric abbreviation.
  167.                 If (Mid(words(i), termlen, 1) = "m") Then
  168.                         match = 1
  169.                         ' Ask the recognizer site for a property bag.
  170.                         Set propbag = RecognizerSite.GetNewPropertyBag
  171.                         ' Write the name of the unit.
  172.                         propbag.Write "unit", "meters"
  173.                         ' Setting of the style property demonstrates how properties
  174.                         ' of a document viewed in a web browser can be controlled.
  175.                         ' When this smart tag is viewed in IE the text color will be
  176.                         ' navy. Note that you could also put an onclick event here or
  177.                         ' set other properties that will change the behavior within IE.
  178.                         ' But, it is almost always better to be conservative or else you will annoy the user.
  179.                         propbag.Write "style", "color:navy"
  180.                 ElseIf (Mid(words(i), termlen, 1) = "l") Then
  181.                         match = 1
  182.                         Set propbag = RecognizerSite.GetNewPropertyBag
  183.                         propbag.Write "unit", "liters"
  184.                         propbag.Write "style", "color:red"
  185.                 End If
  186.                 ' If there is a match, set the values in the property bag and commit the smart tag.
  187.                 ' The contents of the property bag will be used by the actions for metric units and
  188.                 ' will also come in handy when manipulating smart tags with the Word or Excel object model.
  189.                 If (match = 1) Then
  190.                     ' If the string starts with a digit and ends with a metric unit,
  191.                     ' then check if all the intervening characters are also digits.
  192.                     If (termlen > 1) Then
  193.                         For j = 2 To (termlen - 1)
  194.                             If (Mid(words(i), j, 1) < "0" Or Mid(words(i), j, 1) > "9") Then
  195.                                 match = 0
  196.                             End If
  197.                         Next
  198.                     End If
  199.                     If (match = 1) Then
  200.                         quantity = Mid(words(i), 1, termlen - 1)
  201.                         propbag.Write "quantity", quantity
  202.                         ' Commit the smart tag to the application.
  203.                         RecognizerSite.CommitSmartTag METRICURI, index, termlen, propbag
  204.                     Else
  205.                         Set propbag = Nothing
  206.                     End If
  207.                 End If
  208.             End If
  209.         Next
  210.     Else
  211.         ' Do nothing. The only DataType values currently supported are cell and para
  212.     End If
  213. Exit Sub
  214.  
  215. ' Handle whatever errors might occur
  216. bail:
  217.   Err.Clear
  218.   Resume Next
  219.   
  220. End Sub
  221.